﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace SYFranework
{
    public abstract class BaseUI : MonoBehaviour
    {
        private GameObject _cacheObj;
        public GameObject CacheObj
        {
            get {
                if (null == _cacheObj)
                {
                    _cacheObj = this.gameObject;
                }
                return _cacheObj;
            }
        }
        private Transform _transform;
        
        public Transform Transform
        {
            get
            {
                if (null == _transform)
                {
                    _transform = this.transform;
                }
                return _transform;
            }
        }

        protected UIState _state = UIState.None;
        public event StateChangeEvent StateChanged;

        public UIState State
        {
            protected get
            {
                return this._state;
            }
            set
            {
                UIState oldState = this._state;
                if (null != StateChanged)
                {
                    StateChanged(this, oldState, this.State);
                }
                this._state = value;
            }
        }
        //让子类 重写获取UI状态的方法
        public abstract UIType GetUIType();


        private void Awake()
        {
            // 设置状态为初始化状态
            this.State = UIState.Init;
            OnAwake();
        }

        void Start()
        {
            OnStart();
        }

      
        void Update()
        {
            if (this._state == UIState.Ready)
            {
                OnUpdate(Time.deltaTime);
            }  
        }

        void Release()
        {
            this.State = UIState.Closing;
            GameObject.Destroy(this.CacheObj);
            OnRelease();
        }
        private void OnDestroy()
        {
            this.State = UIState.None;
        }

        protected virtual void OnStart()
        {

        }
        protected virtual void OnAwake()
        {
            this.State = UIState.Loading;
            // 播放背景音乐
            this.OnPlayOpenUIAudio();
        }
        protected virtual void OnUpdate(float time)
        {

        }
        protected virtual void OnRelease()
        {
            this.State = UIState.None;
            // 关闭背景音乐
            this.OnPlayCloseUIAudio();
        }
        protected virtual void OnLoadData()
        {
          
        }

        /// <summary>
        ///  播放音乐
        /// </summary>
        protected virtual void OnPlayOpenUIAudio()
        {

        }
        /// <summary>
        /// 关闭音乐
        /// </summary>
        private void OnPlayCloseUIAudio()
        {
            throw new NotImplementedException();
        }

        protected virtual void SetUI(params object[] uiParams)
        {
            this.State = UIState.Loading;
        }

        protected virtual void SetUIWhenOpening(params object[] uiParams)
        {
            SetUI(uiParams);
            StartCoroutine(LoadDataAsyn());
        }

        private IEnumerator LoadDataAsyn()
        {
            yield return new WaitForSeconds(0);
            if (this.State == UIState.Loading)
            {
                this.OnLoadData();
                this.State = UIState.Ready;         
            }
                
        }

       
    }
}
